package cn.wolfcode.crm.service.impl;

import cn.wolfcode.crm.domain.Employee;
import cn.wolfcode.crm.domain.Signin;
import cn.wolfcode.crm.mapper.SigninMapper;
import cn.wolfcode.crm.query.QueryForObject;
import cn.wolfcode.crm.service.ISigninService;
import cn.wolfcode.crm.util.CustomException;
import cn.wolfcode.crm.util.PageResult;
import cn.wolfcode.crm.util.PunchCard;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Set;

@Service
public class SigninServiceImpl implements ISigninService {

    //签到时间
    private static final String BEGINTIME = "09:00:00";
    //签退时间(防早退)
    private static final String ENDTIME = "18:00:00";

    @Autowired
    private SigninMapper mapper;

    public void insert(Signin record) {
        mapper.insert(record);
    }

    public List<Signin> selectAll() {
        return mapper.selectAll();
    }

    public Signin selectByPrimaryKey(Long id) {
        return mapper.selectByPrimaryKey(id);
    }

//    签到
    public void signinTime(Signin signin, Employee employee)
    {
        if(signin == null)//为null证明第一天签到，直接进行判断
        {
            checkIn(employee);
        }
        else
        {
            Date date = new Date();
            Date signIn = signin.getSignIn();
            //            需要进行判断是否有值
            if(signIn == null)
            {
                checkIn(employee);//欢迎回来
            }

            SimpleDateFormat sp = new SimpleDateFormat("yyyy-MM-dd");

            String formatTime = sp.format(signIn);
            String dateTime = sp.format(date);
            //证明不是同一天
            if(dateTime.compareTo(formatTime) != 0)
            {
                checkIn(employee);//欢迎回来
            }
            else
            {

                throw new CustomException("亲,你今天已经签过到了哦");
            }
        }
    }

    public void checkIn(Employee employee)
    {

        Signin signin = new Signin();
        signin.setEmployee(employee);

        //获取到当前的登录时间
        Date time = new Date();

        SimpleDateFormat sp = new SimpleDateFormat("HH:mm:ss");
        String formatTime = sp.format(time);

        /*
        如果参数字符串等于此字符串，则返回值 0；
        如果此字符串按字典顺序小于字符串参数，则返回一个小于 0 的值；9<15 ? -1
        如果此字符串按字典顺序大于字符串参数，则返回一个大于 0 的值。9>8 ? 1
        */
        if(BEGINTIME.compareTo(formatTime) >= 0)
        {
            signin.setInState(1);//签到
        }
        else
        {
            signin.setInState(0);//迟到
        }

        signin.setSignIn(time);

        mapper.signinTime(signin);
    }

//    根据员工id查找对应的签到记录
    public Signin selectSigninByEmployeeId(Long id, String date) {
        return mapper.selectSigninByEmployeeId(id, date);
    }

    //签退
    public void signoutTime(Signin signin, Employee employee, String dateTime) {

        if(signin == null)//证明从未进行过签到，所以就不能进行签退操作
        {
            throw new CustomException("亲,你今天还没签到呢");
        }

//        Date date = new Date();
        Date checkIn = signin.getSignIn();//如果存在，获取到登入的时间，进行判断是否是今天的

        SimpleDateFormat sp = new SimpleDateFormat("yyyy-MM-dd");
//        String dateTime = sp.format(date);
        String formatTime = sp.format(checkIn);

        //这里别忘了签退和签到一样只能进行1次
        if(signin.getSignOut() != null)
        {
            throw new CustomException("亲,你今天已经签退过了");
        }
        else
        {
            checkOut(employee, signin);
        }

        /*if(dateTime.compareTo(formatTime) != 0)//证明今天还未进行签到
        {
            throw new CustomException("亲,你今天还没签到呢");
        }
        else
        {
            checkOut(employee, signin);
        }*/
    }

    public void checkOut(Employee employee, Signin signin)
    {

//        Signin signin = new Signin();
        signin.setEmployee(employee);

        //获取到当前的登录时间
        Date time = new Date();

        SimpleDateFormat sp = new SimpleDateFormat("HH:mm:ss");
        String formatTime = sp.format(time);

        /*
        如果参数字符串等于此字符串，则返回值 0；
        如果此字符串按字典顺序小于字符串参数，则返回一个小于 0 的值；9<15 ? -1
        如果此字符串按字典顺序大于字符串参数，则返回一个大于 0 的值。9>8 ? 1
        */
        if(ENDTIME.compareTo(formatTime) <= 0)
        {
            signin.setOutState(1);//签退
        }
        else
        {
            signin.setOutState(0);//早退
        }

        signin.setSignOut(time);

        mapper.signoutTime(signin);
    }

    @Override
    public PageResult query(QueryForObject qo) {
        int total=mapper.queryForCount(qo);
        if(total==0){
            return new PageResult();
        }
        List<Signin> rows=mapper.queryForList(qo);
        return new PageResult(total,rows);
    }

    //根据员工id查询所有的考勤记录
    public PunchCard querySigninByEmployeeId(Long id) {

        int normalIn = 0;//准点签到
        int errorIn = 0;//迟到
        int normalOut = 0;//准点签退
        int errorOut = 0;//早退

        PunchCard punchCard = new PunchCard();

        Set<Signin> signins = mapper.querySigninByEmployeeId(id);
        for (Signin signin : signins) {
            if(signin.getInState() == 1)
            {
                normalIn ++;
            }
            else
            {
                errorIn ++;
            }
            if(signin.getOutState() == 1)
            {
                normalOut ++;
            }
            else
            {
                errorOut ++;
            }
        }

        punchCard.setNormalSignIn(normalIn);
        punchCard.setFaultSignIn(errorIn);
        punchCard.setNormalSignOut(normalOut);
        punchCard.setFaultSignOut(errorOut);

        return punchCard;
    }
}
